$(function () {
    let songArray = getSongs()
    console.log(songArray);
    let ids = []
    songArray.forEach(function (obj) {
        ids.push(obj.id)
    })
    /* 公共头部处理 */
    $(".go-back").click(function () {
        window.history.back()
    })
    /* 公共底部处理 */
    $(".footer-bottom .list").click(function () {
        $(".modal-top>p>span").html(`列表循环(${songArray.length})`)
        $(".modal-top .clear-all").click(function () {
            clearSongs()
            window.location.href = "./../home/index.html"
        })

        if ($(".modal-midlle>li").length !== songArray.length) {
            $(".modal-midlle").html("");
            songArray.forEach(function (obj) {
                ids.push(obj.id)
                let $li = $(`<li>
                                <p>${obj.name} - ${obj.singer}</p>
                                <img src="images/player-it666-close.png" class="delete-song">
                            </li>`)
                $(".modal-midlle").append($li)
            })
            // 监听歌曲列表删除按钮点击
            $(".delete-song").click(function () {
                // 1.删除sessionStorage中的数据
                let delIndex = $(this).parent().index();
                let len = deleteSongByIndex(delIndex);
                // 如果没有歌曲调整到首页
                if(len === 0){
                    $(".modal-top .clear-all").click();
                }
                // 2.删除UI界面上的数据
                $(this).parent().remove();
                // 3.从Swiper中删除对应的slide
                mySwiper.removeSlide(delIndex);
                // 4.从当前保存的数据中删除对应的数据
                songArray.splice(delIndex, 1);
                // 5.更新歌曲列表头部信息
                $(".modal-top>p>span").html(`列表循环(${len})`);
            });
        }
        $(".modal").css({ display: "block" })
    })
    $(".modal-bottom").click(function () {
        $(".modal").css({ display: "none" })

    })
    /* 处理公共的内容区域 */
    let oAudio = document.querySelector("audio")
    //1.初始化碟片区域
    let mySwiper = new Swiper('.swiper', {
        loop: true
    })

    //3.实现默认界面和歌词界面切换
    $(".main-in").click(function () {
        $(this).toggleClass("active")
        if ($(this).hasClass("active")) {
            getLyric(ids[0])
        }
    })
    //4.监听播放按钮点击
    let flag = false
    $("audio").on("canplay", function () {
        let timeObj = formartTime(oAudio.duration * 1000)
        $(".total-time").text(timeObj.minute + ":" + timeObj.second)
        if (!flag) {
            flag = true

        } else {
            return
        }
        $(".play").click(function () {
            if ($(this).attr("class").includes("active")) {
                $(".disc-pic").css({ "animation-play-state": " paused" })
                $("audio")[0].pause()
            } else {
                $(".disc-pic").css({ "animation-play-state": " running" })
                $("audio")[0].play()
            }
            $(this).toggleClass("active")
        })
    })
    //5.获取歌曲信息


    MusicApis.getSongDetail(ids.join(","))
        .then(function (data) {
            console.log(data);
            for (let i = 0; i < data.songs.length; i++) {
                let song = data.songs[i]
                if (i === 0) {
                    $(".header-title").text(song.name)
                    let singer = ""
                    for (let j = 0; j < song.ar.length; j++) {
                        if (j === 0) {
                            singer = song.ar[0].name
                        } else {
                            singer += "/" + song.ar[0].name
                        }
                    }
                    $(".header-singer").text(singer);
                    $(".disc-pic>img").attr("src", song.al.picUrl)
                }
            }
        })
        .catch(function (err) {
            console.log(err);
        })
    //获取歌曲的url
    MusicApis.getSongURL(ids[0])
        .then(function (data) {
            for (let i = 0; i < data.data.length; i++) {
                let $sc = $(`<source src="${data.data[i].url}" type="audio/${data.data[i].type}"/>`)
                $("audio").append($sc)
            }
        })
        .catch(function (err) {
            console.log(err);
        })
    //控制音乐进度条
    let musicProgress = new NJProgress($(".progress-bar"), $(".progress-line"), $(".progress.dot"))
    musicProgress.progressClick(function (value) {
        oAudio.currentTime = value * oAudio.duration
    })
    musicProgress.progressMove(false, function (value) {
        oAudio.currentTime = value * oAudio.duration
    })
    $("audio").on("timeupdate", function () {
        //处理当前播放时间
        let timeObj = formartTime(oAudio.currentTime * 1000)
        $(".cur-time").text(timeObj.minute + ":" + timeObj.second)
        //处理进度条同步
        let value = oAudio.currentTime / oAudio.duration * 100
        musicProgress.setProgress(value)
        //处理歌词同步
        let curTime = parseInt(oAudio.currentTime)
        let cur$li = $("#nj_" + curTime)
        if (!cur$li[0]) return
        $("#nj_" + curTime).addClass("active").siblings().removeClass("active")
        let curOffset = cur$li[0].lrc.offset
        if ($(".lyric-list")[0].isDrag) return
        lyricScroll.scrollTo(0, curOffset)
    })

    //控制音量
    let volumeValue = 0.5
    oAudio.volume = volumeValue
    let voiceProgress = new NJProgress($(".voice-progress-bar"), $(".voice-progress-line"), $(".voice-progress-dot"))
    voiceProgress.progressClick(function (value) {
        oAudio.volume = value
    })
    voiceProgress.progressMove(false, function (value) {
        oAudio.volume = value
        volumeValue = value
    })
    //音量按钮点击
    $(".lyric-top>img").click(function (event) {
        if (oAudio.volume === 0) {
            oAudio.volume = volumeValue
            voiceProgress.setProgress(volumeValue * 100)
        } else {
            oAudio.volume = 0
            voiceProgress.setProgress(0)
        }
        return event.stopPropagation()
    })

    //2.初始化歌词滚动
    let lyricScroll = new IScroll(".lyric-bottom", {
        mouseWheel: false,
        scrolbars: false,
        probeType: 3
    })
    lyricScroll.on("scroll", function () {
        $(".lyric-time-line").css({ display: "flex" })
        $(".lyric-list")[0].isDrag = true
        let index = Math.abs(parseInt(this.y / $(".lyric-list>li").eq(0).height()))
        let cur$li = $(".lyric-list>li").eq(index)
        if (!cur$li[0]) return
        $(".lyric-time-line>span").text(cur$li[0].lrc.timeStr)
        cur$li.addClass("hover").siblings().removeClass("hover")
    })
    lyricScroll.on("scrollEnd", function () {
        setTimeout(function () {
            $(".lyric-list")[0].isDrag = false
            $(".lyric-time-line").css({ display: "none" })

        }, 3000)

    })
    function getLyric(id) {
        MusicApis.getSongLyric(id)
            .then(function (data) {
                let lyricObj = parseLyric(data.lrc.lyric)
                let index = 0
                for (let key in lyricObj) {
                    let $li = $(`<li id="nj_${key}">${lyricObj[key]}</li>`)
                    if (index === 0) {
                        $li.addClass("active")
                    }
                    $(".lyric-list").append($li)
                    let li = $li[0]
                    let timeObj = formartTime(key * 1000)
                    li.lrc = {
                        offset: -index * $li.height(),
                        timeStr: timeObj.minute + ":" + timeObj.second
                    }
                    index++
                }
                lyricScroll.refresh()
                lyricScroll.maxScrollY -= $(".lyric-bottom").height()
            })
            .catch(function (err) {
                console.log(err);
            })
    }

    function parseLyric(lrc) {
        let lyrics = lrc.split("\n")
        // 1.定义正则表达式提取
        let reg1 = /\[\d*:\d*\.\d*\]/g
        //2.定义正则表达式提取[00
        let reg2 = /\[\d*/i
        //3.定义正则表达式提取 :00
        let reg3 = /\:\d*/i
        //4.定义对象保存处理好的歌词
        let lyricObj = {}
        lyrics.forEach(function (lyric) {
            //1.提取时间
            let timeStr = lyric.match(reg1)
            if (!timeStr) return
            timeStr = timeStr[0]
            //2.提取分钟
            let minStr = timeStr.match(reg2)[0].substr(1)
            //3.提取秒钟
            let secondStr = timeStr.match(reg3)[0].substr(1)
            //4.合并时间
            let time = parseInt(minStr) * 60 + parseInt(secondStr)
            //5.处理歌词
            let text = lyric.replace(reg1, "").trim()
            //6.保存数据
            lyricObj[time] = text
        })
        return lyricObj
    }
})